package com.yanfei.zero.apis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yanfei.zero.application.handler.secret.IgnoreDecrypt;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

@RestController

@Tag(name="Redis相关功能")
//@RequiredArgsConstructor
//@CacheConfig(cacheNames = "user")
public class RedisController {


//	final DeveloperProperty developerProperty;
//	final User1Repository user1Repository;
	@Resource
	RedisTemplate<String,Object> redisTemplate;
	
	@Operation(summary = "添加缓存")
    @PostMapping("/redis")
    @IgnoreDecrypt
    @Cacheable(cacheNames = "data",key = "1")
    Object redis() {
        // persisting the user
		Map<String,Object> data = new HashMap<>();
		data.put("name", "tets1");
        return data;
    }
	
	@Operation(summary = "删除缓存")
    @PostMapping("/redisDel")
    @IgnoreDecrypt
    @CacheEvict(key = "1",cacheNames = "data" )
    Object redisDel() {
        // persisting the user
		Map<String,Object> data = new HashMap<>();
		data.put("name", "tets1");
        return data;
    }
	@Operation(summary = "计算距离")
	@PostMapping("/redisDistance")
	@IgnoreDecrypt
	Object redisDistance() {
		// persisting the user
		Map<String,Object> data = new HashMap<>();

		// 1 add：添加一个或多个地理位置到指定的Geo键中
		redisTemplate.opsForGeo().add("mygeo", new Point(116.397128, 39.916527), "Beijing");
		redisTemplate.opsForGeo().add("mygeo", new Point(121.472641, 31.231707), "Shanghai");
		redisTemplate.opsForGeo().add("mygeo", new Point(113.264435, 23.129163), "Guangzhou");

		// 2 position：获取指定成员的地理位置
		List<Point> points = redisTemplate.opsForGeo().position("mygeo", "Beijing");
		data.put("points", points);
		
		// 3 distance：计算两个成员之间的距离（默认以米为单位）
		Distance distance = redisTemplate.opsForGeo().distance("mygeo", "Beijing", "Shanghai");
		double distanceInKm = distance.getValue() / 1000;
		data.put("distanceInKm", distanceInKm);
		
		
		// 4 hash：获取指定成员的Geohash值
		List<String> geohash = redisTemplate.opsForGeo().hash("mygeo", "Beijing");
		data.put("geohash", geohash);
		
		// 5 radius：根据给定的中心点，返回与中心点距离在指定范围内的成员（按距离由近到远排序）
		Circle circle = new Circle(new Point(116.397128, 39.916527), new Distance(200, Metrics.KILOMETERS));
		GeoResults<GeoLocation<Object>> geoResults = redisTemplate.opsForGeo().radius("mygeo", circle);
		data.put("geoResults", geoResults);
		
		// radiusByMember：根据给定的成员，返回与该成员距离在指定范围内的其他成员（按距离由近到远排序）
		GeoResults<GeoLocation<Object>> geoResult = redisTemplate.opsForGeo().radius("mygeo", "Beijing", new Distance(200, Metrics.KILOMETERS));
		data.put("geoMemberResults", geoResult);
		
		
	   // 7 remove：从指定的Geo键中移除一个或多个成员
		Long removedMembers = redisTemplate.opsForGeo().remove("mygeo", "Beijing", "Shanghai");
		data.put("removedMembers", removedMembers);
		
		return data;
	}
    
}
